<?php
/*
* IKPHP爱客网 安装程序 @copyright (c) 2012-3000 IKPHP All Rights Reserved @author 小麦
* @Email:ikphp@sina.cn
* @小麦 关注 2014年8月9日12:11:41
*/
namespace IKPHP\Apps\Space\Models;

use IKPHP\Common\Models\BaseModel;
use IKPHP\Common\Models\User;
use IKPHP\Common\Models\Images;

class UserNote extends BaseModel {
	
	public $noteid;
	public $userid;	
	
	public function getSource() {
		return IK . "user_note";
	}
    /* 用户模型自动完成 */	
    public function beforeCreate()
    {

        $this->count_comment 	= 0;
        $this->count_view	= 0;
        $this->count_like	= 0;
        $this->count_recommend	= 0;

        $this->isaudit	= 0;
        $this->isreply	= 0;
        $this->isrecommend	= 0;

		$this->addtime	= time();
        $this->uptime	= time();
    }	
	//最多浏览最多的
	public static function getHotNotes($userid='',$limit=10){
		if(!empty($userid)){
			$conditons = "userid='$userid' and isaudit = 1 and privacy = 1";
		}else{
			$conditons = "isaudit = 1 and privacy = 1";
		}
		$where = array($conditons,'order'=>'count_view DESC', 'limit'=> $limit);
		$res = self::find($where);
		if($res){
			foreach($res->toArray() as $key=>$item){
				$result[] = $item;
			}
			return $result;
		}else{
			return false;
		}
	}
	//获取推荐日记
	public static function getRecommendNote($limit = 10){
		$where = array("isrecommend = 1",'order'=>'addtime DESC', 'limit'=> $limit);
		$res = self::find($where);
		if($res){
			foreach($res->toArray() as $key=>$item){
				$result[] = $item;
				$result[$key]['user'] = User::getOneUser($item['userid']);
				$result[$key]['content'] = clearText($item['content']);
			}
			return $result;
		}else{
			return false;
		}
	}
	//获取指定条数的日记
	public static function getNewNotes($userid, $limit = 10){
		$conditions = "userid = '$userid' and title <> '0' and isaudit =1 and privacy = 1";
		$result = self::find(array($conditions, 'order'=>'addtime desc', 'limit'=> $limit));
		if (count($result)>0){
			return $result->toArray();
		}else{
			return false;		
		}
	}
	//删除操作
	public function afterDelete(){
		//删除图片
		Images::find("userid = '$this->userid' and type='note' and typeid='$this->noteid'")->delete();
	}
	//获取一篇
	public static function getOneNote($noteid) {
		$where['conditions'] = " noteid = '$noteid' AND title <>'0'";
		$strNote = array();
		$result = self::findFirst($where); 
		if($result){			
			$strNote = $result->toArray();
			$content = $strNote['content'];
			$strNote['content'] = ikhtml('note', $noteid, $content,1);
			$strNote['content_photo'] = ikhtml_img('note', $noteid, $content);
			if(!empty($strNote['content_photo'])){
				$strNote['headimg'] = $strNote['content_photo'][0]['simg'];			
			}
		}
		//var_dump($strTopic['headimg']);die;
		return $strNote;
	}
}
